package cn.lingyangwl.agile.infra.dict.controller;

import cn.lingyangwl.agile.infra.dict.service.DictService;
import cn.lingyangwl.agile.infra.module.group.model.rqrs.DataGroupDeleteReq;
import cn.lingyangwl.agile.infra.module.group.model.rqrs.DataGroupSaveReq;
import cn.lingyangwl.agile.infra.module.group.model.rqrs.DataGroupUpdateReq;
import cn.lingyangwl.agile.infra.group.service.DataGroupService;
import cn.lingyangwl.agile.model.enums.DataGroupBizTypeEnum;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import cn.lingyangwl.framework.core.response.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 字典组
 * 
 * @author shenguangyang
 */
@Api(tags = "字典组")
@RestController
@RequestMapping("/dictGroup")
public class DictGroupController {
    @Resource
    private DataGroupService dataGroupService;
    @Resource
    private DictService dictService;

    @ApiOperation(value = "保存字典组")
    @PostMapping("/save")
    @RamResource(action = "dict:group:save", name = "保存字典组", type = ActionType.WRITE)
    public Resp<Long> saveDataGroup(@RequestBody DataGroupSaveReq req) {
        req.setBizType(DataGroupBizTypeEnum.DICT.getType());
        return Resp.ok(dataGroupService.saveGroup(req));
    }
  
    @ApiOperation(value = "更新字典组")
    @PostMapping("/update")
    @RamResource(action = "dict:group:update", name = "更新字典组", type = ActionType.WRITE)
    public Resp<?> updateDataGroup(@RequestBody DataGroupUpdateReq req) {
        return Resp.ok(dataGroupService.updateGroup(req));
    }
  
    @ApiOperation(value = "批量删除字典组")
    @DeleteMapping("/batchDelete")
    @RamResource(action = "dict:group:delete", name = "删除字典组", type = ActionType.WRITE)
    public Resp<?> batchDeleteDataGroup(@RequestParam(value = "ids") List<Long> ids) {
        DataGroupDeleteReq req = new DataGroupDeleteReq();
        req.setBizType(DataGroupBizTypeEnum.DICT.getType());
        req.setIds(ids);
        return Resp.ok(dictService.deleteGroup(req));
    }
  
    @ApiOperation(value = "删除字典组")
    @DeleteMapping("/delete")
    @RamResource(action = "dict:group:delete", name = "删除字典组", type = ActionType.WRITE)
    public Resp<?> deleteDataGroup(@RequestParam(value = "id") Long id) {
        DataGroupDeleteReq req = new DataGroupDeleteReq();
        req.setBizType(DataGroupBizTypeEnum.DICT.getType());
        req.setIds(Collections.singletonList(id));
        return Resp.ok(dictService.deleteGroup(req));
    }
}
